package cn.com.zhbs.account.controller;

import cn.com.zhbs.account.service.impl.AccountServiceImpl;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import cn.com.zhbs.account.entitys.Account;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: vr_manage
 * @description: 账户管理
 * @author: 陈治杰
 * @create: 2021-08-06 14:06
 **/

@Controller
@RequestMapping("account")
public class AccountController {
	@Resource
	private AccountServiceImpl accountsService;

	//新增用户
	@RequestMapping(value = "addAccount", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> addAccount(@RequestBody JSONObject jsonObject){
		try{
			return accountsService.addAccount((jsonObject));
		}catch (Exception e){
			e.printStackTrace();
		}

		return ResultFactory.failure().display();
	}

	//用户列表
	@RequestMapping(value="accountList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> accountList(@RequestBody JSONObject jsonObject,HttpServletRequest request, HttpServletResponse servletResponse){
		HttpSession session=request.getSession();
		try{
			jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
			jsonObject.put(Account.superRole.toString(),session.getAttribute(Account.superRole.toString()));
			Map<String,Object> result=accountsService.accountList(jsonObject);
			return ResultFactory.success(result).display();
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultFactory.failure().display();
	}

	//用户详情
	@RequestMapping(value = "accountDetail",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> accountDetail(@RequestBody JSONObject jsonObject,HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,Object> result=accountsService.accountDetail(jsonObject);
			return ResultFactory.success(result).display();
		}catch (Exception e){
			e.printStackTrace();
			return ResultFactory.failure().display();
		}
	}

	//编辑用户
	@RequestMapping(value = "accountEdit" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> accountEdit(@RequestBody JSONObject jsonObject,HttpServletRequest request){
		try {
				return accountsService.editAccount(jsonObject);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultFactory.failure().display();
	}

	//修改密码(需要旧密码)
	@RequestMapping(value = "changePassword",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> changePassword(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response){
		HttpSession session=request.getSession();
		try{
			jsonObject.put(Account.aid.toString(),session.getAttribute(Account.aid.toString()));
			return accountsService.editPassword(jsonObject);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultFactory.failure().display();
	}


	//禁用启用账户
	@RequestMapping(value = "accountStatus" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String,Object> accountStatus(@RequestBody JSONObject jsonObject,HttpServletRequest request) {
		try{
			accountsService.updateAccountStatus(jsonObject);
			return ResultFactory.success().display();
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResultFactory.failure().display();
	}
}
